package com.xqkad.myplayer.activity;

import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.wm.remusic.widget.SildingFinishLayout;
import com.xqkad.myplayer.R;
import com.xqkad.myplayer.service.BroadcastService;
import com.xqkad.myplayer.service.MusicPlayer;

public class LockActivity extends ReceiverActivity implements View.OnClickListener {
    private TextView tv1,tv2;
    private ImageView imgPrev,imgPlay,imgNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindow();
        setContentView(R.layout.activity_lock);
        getSupportActionBar().hide();
        init();
        updateUI();
    }

    void init(){
        SildingFinishLayout mView = (SildingFinishLayout) findViewById(R.id.lock_root);
        mView.setOnSildingFinishListener(new SildingFinishLayout.OnSildingFinishListener() {

            @Override
            public void onSildingFinish() {
                finish();
            }
        });
        mView.setTouchView(getWindow().getDecorView());
        tv1 = (TextView) findViewById(R.id.lock_ac_text1);
        tv2 = (TextView) findViewById(R.id.lock_ac_tv2);
        imgPrev = (ImageView) findViewById(R.id.lock_ac_pre);
        imgPlay = (ImageView) findViewById(R.id.lock_ac_play);
        imgNext = (ImageView) findViewById(R.id.lock_ac_next);
        imgPrev.setOnClickListener(this);
        imgPlay.setOnClickListener(this);
        imgNext.setOnClickListener(this);
    }

    private void setWindow(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);//api 27后
        }else{
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);//api 27前
        }
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                         View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav
                        // bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lock_ac_pre:
                MusicPlayer.previous();
                break;
            case R.id.lock_ac_play:
                if(MusicPlayer.isPlaying())
                    MusicPlayer.pause();
                else MusicPlayer.play();
                break;
            case R.id.lock_ac_next:
                MusicPlayer.next();
                break;
        }
    }

    @Override
    protected void onReceive(Intent intent) {
        final String action = intent.getAction();

        if(ON_PLAY.equals(action)||
                ON_PAUSE.equals(action)||
                ON_STOP.equals(action)){
            updateUI();
        }
    }

    // call on onReceive onCreate
    void updateUI(){
        tv1.setText("");
        tv2.setText(MusicPlayer.getTitle());
        if(MusicPlayer.isPlaying()){
            imgPlay.setImageResource(R.drawable.play_btn_pause);
        }else{
            imgPlay.setImageResource(R.drawable.play_rdi_btn_play);
        }
    }
}
